﻿///////////////////////////////////////////////////////////////////////////////
// File:        Acceptor.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef NIO_NET_ACCEPTOR_HPP_
#define NIO_NET_ACCEPTOR_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#include <boost/function.hpp>
#include <nio/net/EventHandler.hpp>
#include <nio/net/Socket.hpp>
#include <nio/net/Connection.hpp>

namespace nio
{
    namespace net
    {
        class Acceptor : public EventHandler
        {
        public:
            enum State
            {
                READY,
                RUNNING,
                EXITING,
                EXIT
            };
        public:
            Acceptor(void);
            virtual ~Acceptor(void);

        public:
            bool open(std::string addr, int port, int backlog);
            void shutdown(void)
            {
                sock_.close();
                setState(EXIT);
            }

            Socket& getSocket(void) { return sock_; }
            State getState(void) const { return state_; }

            void setCloseFN(boost::function<void(void)> fn) { closeFN_ = fn; }
            void setConnectionAcceptFN(boost::function<void(sock_t)> fn) { connAcceptFN_ = fn; }
            
        public:
            virtual void handleInput(void);
            virtual void handleOutput(void);
            virtual void handleError(void);
            virtual void handleHeartbeat(void);

        private:
            void setState(State state) { state_ = state; }

        private:
            State                           state_;
            Socket                          sock_;
            bool                            cnfLinger_;

            boost::function<void(sock_t)>   connAcceptFN_;
            boost::function<void(void)>     closeFN_;
        };
    }
}

#endif //NIO_NET_ACCEPTOR_HPP_
